/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Mr.Nattasan Wang-Aphisit
 * @version 1.1
 */

package wads.app;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import wads.com.WADTools;

public class WADConfigDialog extends JDialog {
  
    private Properties properties;
    private JLabel webserverLabel = new JLabel("Web Server:");
    private JTextField webserverField = new JTextField();
    private JButton okBtn = new JButton("OK");
    private JButton cancelBtn = new JButton("Cancel");
    private JPanel buttonsPanel = new JPanel();
    private String server = "";
    private String action = "";
    private String dbserver = "";
    private JLabel dbserverLabel = new JLabel("Database Server:");
    private JTextField dbserverField = new JTextField();
    private JCheckBox dbCheckBox = new JCheckBox("Change database server URL.");
    
    private File configFile = new File(WADTools.CONFIG_FILENAME);
    private JLabel servletactLabel = new JLabel("Servlet Action:");
    private JCheckBox webserverCheckBox = new JCheckBox("Change server URL.");
    private JTextField actionField = new JTextField();
    private JCheckBox actionCheckBox = new JCheckBox("Set servlet action URL.");

  public WADConfigDialog(WADDiagramEditor owner, boolean modal) {
    super(owner, modal);
    try {
      jbInit();
    }
    catch(Exception e) { e.printStackTrace(); }
  }

  private void jbInit() throws Exception {
    this.setTitle("Configuration");
    this.setSize(450, 350);
    this.setResizable(false);

    webserverLabel.setBounds(new Rectangle(10, 37, 81, 26));
    servletactLabel.setBounds(new Rectangle(10, 118, 110, 26));
    dbserverLabel.setBounds(new Rectangle(10, 196, 130, 26));

    okBtn.setActionCommand("okBtn");
    okBtn.setBounds(new Rectangle(51, 7, 51, 27));
    okBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        okBtn_actionPerformed(e);
      }
    });

    cancelBtn.setActionCommand("cancelBtn");
    cancelBtn.setBounds(new Rectangle(112, 6, 73, 27));
    cancelBtn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        cancelBtn_actionPerformed(e);
      }
    });

    buttonsPanel.setLayout(null);
    buttonsPanel.setBounds(new Rectangle(47, 260, 215, 44));

    this.getContentPane().setLayout(null);

    webserverField.setEnabled(false);
    webserverField.setBounds(new Rectangle(130, 40, 185, 25));
    webserverCheckBox.setBounds(new Rectangle(9, 70, 150, 25));
    webserverCheckBox.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        serverCheckBox_actionPerformed(e);
      }
    });

    actionField.setEnabled(false);
    actionField.setBounds(new Rectangle(130, 120, 185, 25));
    actionCheckBox.setBounds(new Rectangle(9, 150, 180, 25));
    actionCheckBox.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        actionCheckBox_actionPerformed(e);
      }
    });

    dbserverField.setEnabled(false);
    dbserverField.setBounds(new Rectangle(130, 200, 185, 25));
    dbCheckBox.setBounds(new Rectangle(9, 230, 220, 25));
    dbCheckBox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            dbCheckBox_actionPerformed(e);
        }
    });

    buttonsPanel.add(cancelBtn, null);
    buttonsPanel.add(okBtn, null);
    this.getContentPane().add(buttonsPanel, null);
    this.getContentPane().add(servletactLabel, null);
//    this.getContentPane().add(dbserverLabel, null);
//    this.getContentPane().add(dbserverField, null);
//    this.getContentPane().add(dbCheckBox, null);
    this.getContentPane().add(actionField, null);
    this.getContentPane().add(actionCheckBox, null);
    this.getContentPane().add(webserverCheckBox, null);
    this.getContentPane().add(webserverLabel, null);
    this.getContentPane().add(webserverField, null);
  }

  public void loadPropertiesToDialog(Properties prop){
    this.properties = prop;
    server = properties.getProperty(WADTools.SERVER_NAME_KEYNAME, WADTools.SERVER_NAME_DEFAULT);
    action = properties.getProperty(WADTools.ACTION_NAME_KEYNAME, WADTools.ACTION_NAME_DEFAULT);
    dbserver = properties.getProperty(WADTools.DBSERVER_NAME_KEYNAME, WADTools.DBSERVER_NAME_DEFAULT);

    // load properties into Field
    webserverField.setText(server);
    actionField.setText(action);
    dbserverField.setText(dbserver);
  }

  private void saveProperties(){
    String serverTemp = webserverField.getText();
    String actionTemp = actionField.getText();
    String dbTemp = dbserverField.getText();
    if((server.compareTo(serverTemp) != 0) || (action.compareTo(actionTemp) != 0 ) || (dbserver.compareTo(dbTemp) != 0)){
      properties.setProperty(WADTools.SERVER_NAME_KEYNAME, serverTemp);
      properties.setProperty(WADTools.ACTION_NAME_KEYNAME, actionTemp);
      properties.setProperty(WADTools.DBSERVER_NAME_KEYNAME, dbTemp);

      try{
        properties.store(new FileOutputStream(configFile), "WADS' properties.");
        WADTools.initialSharingResource();
        System.out.println("Data is Changed.");
      }
      catch(IOException e) {
          System.out.println("Can't save configuration");
          e.printStackTrace(System.err);
      }
    }
  }

  private void okBtn_actionPerformed(ActionEvent e) {
    saveProperties();
    setVisible(false);
    this.dispose();
  }

  private void cancelBtn_actionPerformed(ActionEvent e) {
    setVisible(false);
    this.dispose();
  }

  private void serverCheckBox_actionPerformed(ActionEvent e) {
    if(webserverCheckBox.isSelected())
      webserverField.setEnabled(true);
    else
      webserverField.setEnabled(false);
  }

  private void actionCheckBox_actionPerformed(ActionEvent e) {
    if(actionCheckBox.isSelected())
      actionField.setEnabled(true);
    else
      actionField.setEnabled(false);
  }

  private void dbCheckBox_actionPerformed(ActionEvent e) {
      if(dbCheckBox.isSelected())
          dbserverField.setEnabled(true);
      else
          dbserverField.setEnabled(false);
  }

}